<?php
/*
 * Created on Oct 11, 2011
 */
 class AdminLog extends MX_Controller {
	function AdminLog()
	{
		parent::__construct();	
		$this->load->model('public_model');
	}
	
	function index()
	{
		if(modules::run('login/is_admin_logged_in') == false ){
			redirect('login');
			return;
		}
		$vars = array();
		$content = $this->load->view('adminlog_view', $vars,true);
		Template::display($content);
	}
	function saveLog($title,$content){
		$this->load->model('adminlog_model');
		return $this->adminlog_model->saveLog($title,$content);
	}
	function searchAdminLog(){
		if(modules::run('login/is_admin_logged_in') == false ){
			redirect('login');
			return;
		}
		
		if(empty($this->session->userdata['is_logged_in']) || $this->session->userdata['is_logged_in']==false)
		{
			$SURL="Location: ".base_url()."login";
			header($SURL);
			exit();
		}
		$this->load->library('CommonUtils');
		$this->load->model('adminlog_model');
		
		$fromDate = $toDate = "";
		
		if($_POST['dateFrom']!= "0")
		{
			$fromDate = $this->commonutils->changeFormatDate1($_POST['dateFrom']); 
		}
		if($_POST['dateTo']!= "0"){
			$toDate = $this->commonutils->changeFormatDate1($_POST['dateTo']);
		}
		$key = $_POST['key'];
		$errorcode = $_POST['errorcode'];
		$page = $_POST['page'];
		//// validatition
		$errList = "";
		if($fromDate== " ")$errList .= "The From date field must contain a valid date<br/>";
		if($toDate== " ")$errList .= "The To date field must contain a valid date";
		
		if($errList=="" && $fromDate!="" && $toDate!=""){
			$dtFr = new DateTime($fromDate);
			$dtTo = new DateTime($toDate);
			if($dtTo<$dtFr)$errList .= "To date must equal or greater than From date";
		}
		if($errList!=""){
			$vars = array("hasError"=>true,
						  "keysearch"=>$_POST['key'],
						  "frdate"=>$_POST['dateFrom'],
						  "todate"=>$_POST['dateTo'],
						  "errorcodesearch"=>$_POST['errorcodesearch'],
						  "errorList"=>$errList);
		}
		///end validation
		else{
			$numProductPerPage = $this->public_model->getSystemParameterValue("itemsPerPage"); //$this->commonutils->getNumberProductPerPage();
			$limitPage = $this->commonutils->getNumberPageDisplay();
			$startIndex = ($page -1)*$numProductPerPage;
			$endIndex = $page*$numProductPerPage;
			$totalResult = $this->adminlog_model->getTotalResults($fromDate,$toDate,$key,$errorcode);
			$pPaging = array();
			$pPaging = $this->commonutils->paging($page,$numProductPerPage,$totalResult,$limitPage);
			//end paging
			$logs = $this->adminlog_model->searchAdminLogSubmit($fromDate,$toDate,$startIndex,$endIndex,$key,$errorcode);
			$logList = array();
			foreach($logs as $log){
				$logList[]=$log;
			}
			$vars["hasError"] = false;
			$vars["logList"] = $logList;
			$vars["pPaging"] = $pPaging;
		}
		echo json_encode($vars);
	}
	
	function del()
	{
		if(modules::run('login/is_admin_logged_in') == false ){
			redirect('login');
			return;
		}
		
		$listId=$this->input->post('listIds');
		$this->load->model('adminlog_model');
		$delWishList =  $this->adminlog_model->delete_logs($listId);
		echo "Pass"."|".base_url()."searchAdminLog"; 
		
	}
 }
 
?>
